package cn.com.cpic.insurance.rulecheck.core.rule;

import java.util.Set;

import org.apache.el.parser.ELParser;
import org.apache.el.parser.Node;

import cn.com.cpic.insurance.rulecheck.core.AbstractTokenable;
import cn.com.cpic.insurance.rulecheck.core.Context;
import cn.com.cpic.insurance.rulecheck.core.ModifiableSet;
import cn.com.cpic.insurance.rulecheck.core.SimpleModifiableSet;

/**
 * 
 * 
 * @author 代波
 * @since 2011-8-21
 */
public final class SimpleRule extends AbstractTokenable<String> implements Rule {
	private static final long serialVersionUID = 8551015624161669620L;

	private final ModifiableSet<RuleConsumer<?>> ruleConsumerSet;

	private Node node;
	private String bom;
	private Object value;

	private String desc;

	public SimpleRule(String token, String bom, String desc, Object value) {
		this(token, bom, desc, value, (Set<RuleConsumer<?>>) null);
	}

	public SimpleRule(String token, String bom, String desc, Object value, Set<RuleConsumer<?>> ruleConsumerSet) {
		this(token, bom, desc, value, new SimpleModifiableSet<RuleConsumer<?>>(ruleConsumerSet));
	}

	public SimpleRule(String token, String bom, String desc, Object value,
			ModifiableSet<RuleConsumer<?>> ruleConsumerSet) {
		super(token);
		setBom(bom);
		setValue(value);
		setDesc(desc);
		this.ruleConsumerSet = ruleConsumerSet;
	}

	public String getBom() {
		return bom;
	}

	public void setBom(String bom) {
		this.bom = bom;
		this.node = ELParser.parse(bom);
	}

	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		this.value = value;
	}

	public Boolean operate(Context context) {
		node.setValue(context.getEvaluationContext(), value);
		return Boolean.TRUE;
	}

	public ModifiableSet<RuleConsumer<?>> getRuleConsumerSet() {
		return ruleConsumerSet;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}
}
